// pages/member/member.js
const app = getApp()
var config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    industryTypeArray: [],
    industryTypeNameArray: [],
    industryTypeIndex: 0,
    content:'',
    email:'',
    logo:[]
  },
  industryTypePickerChange: function (e) {
    var industryTypeArray = this.data.industryTypeArray;
    var industryTypeName = industryTypeArray[e.detail.value].industryTypeName;
    var industryTypeId = industryTypeArray[e.detail.value].industryTypeId;
    this.setData({
      industryTypeName: industryTypeName,
      industryTypeId: industryTypeId,
      industryTypeIndex: e.detail.value
    })
  },
  add:function(){
    var that = this;
    if (that.data.companyName == undefined || that.data.companyName == null || that.data.companyName == "") {
      app.tip('请输入名称');
      return false;
    }
    if (that.data.industryTypeName == undefined || that.data.industryTypeName == null || that.data.industryTypeName == "") {
      app.tip('请选择行业类型');
      return false;
    }
    if (that.data.linkMan == undefined || that.data.linkMan == null || that.data.linkMan == "") {
      app.tip('请输入负责人');
      return false;
    }
    if (that.data.phone == undefined || that.data.phone == null || that.data.phone == "") {
      app.tip('请输入联系电话');
      return false;
    }
    if (that.data.address == undefined || that.data.address == null || that.data.address == "") {
      app.tip('请输入地址');
      return false;
    }

    var logoUrl = '';
    console.info(that.data.logoImageId);
    console.info(that.data.permitImageId);
    if (that.data.logoImageId != null && that.data.logoImageId != undefined && that.data.logoImageId != '') {
      logoUrl = config.fileSaveUrl + that.data.logoImageId
    }
    console.info(logoUrl);
    var companyName = that.data.companyName;
    var phone = that.data.phone;
    var content = that.data.content;
    var industryTypeId = that.data.industryTypeId;
    var industryTypeName = that.data.industryTypeName;
    var linkMan = that.data.linkMan;
    var email = that.data.email;
    var address = that.data.address;
    var permitUrl = '';
    if (that.data.permitImageId != null && that.data.permitImageId != undefined && that.data.permitImageId != '') {
      permitUrl = config.fileSaveUrl + that.data.permitImageId
    }

    let editData = {
      data: encodeURIComponent('{' +
        '"companyName":"' + companyName + '",' +
        '"phone":"' + phone + '",' +
        '"logoUrl":"' + logoUrl + '",' +
        '"content":"' + content + '",' +
        '"industryTypeId":"' + industryTypeId + '",' +
        '"industryTypeName":"' + industryTypeName + '",' +
        '"linkMan":"' + linkMan + '",' +
        '"email":"' + email + '",' +
        '"address":"' + address + '",' +
        '"permitUrl": "' + permitUrl + '"}')
    }
    wx.request({
      url: config.saveCompanyUrl,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: editData,
      success: function (result) {
        wx.hideLoading();
        console.info("添加企业返回结果");
        console.info(result);
        if (result.data.success == true) {
          //app.tip("添加成功", "none");

          console.info("企业登录")
          var userType = '2';
          wx.request({
            url: config.loginUrl,
            method: 'POST',
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              "userPhone": phone,
              "userType": userType
            },
            success: function (result) {
              console.info(result);
              var data = result.data.object;
              console.info(data);
              if (data != null) {
                console.info("有");
                wx.setStorageSync("workerInfo", null);
                wx.setStorageSync("uid", 1);
                wx.setStorageSync("companyInfo", data);
                that.setData({
                  uid: 1,
                  modalName: null
                })

                wx.navigateTo({
                  url: '../index/index',
                })
                
              } else {
                console.info("无");
                if (result.data.code == '201') {
                  app.tip("该账号是个人账号,不可以登录企业系统", "none");
                } else {
                  wx.navigateTo({
                    url: '../member/member',
                  })
                }
              }
            },
            fail: function ({ errMsg }) {
              wx.hideLoading()
              app.tip(errMsg, 'none')
            }
          })

        } else {
          app.tip(result.data.message, "none");
        }
      },
      fail: function ({ errMsg }) {
        console.info("添加企业信息失败");
        wx.hideLoading();
      },
    });

  },
  ChooseImage() {
    var that = this;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: config.fileUploadUrl,
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type":
              "multipart/form-data"
          },
          success: function (res) {
            console.info("营业执照上传返回结果");
            console.info(res);
            let data = JSON.parse(res.data);
            if (data.success == true) {
              that.setData({
                permitImageId: data.list[0]
              })
            } else {
              app.tip(res.data.message, "none");
            }
            wx.hideLoading();
          },
          fail: function (res) {
            wx.hideLoading();
            console.log('上传失败');
          }
        })

        if (that.data.imgList.length != 0) {
          var imgList = that.data.imgList;
          imgList.splice(0, 1)
          that.setData({
            imgList: res.tempFilePaths
          })
        } else {
          that.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  ChooseLogo() {
    var that = this;
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: config.fileUploadUrl,
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type":
              "multipart/form-data"
          },
          success: function (res) {
            console.info("企业logo上传返回结果");
            console.info(res);
            let data = JSON.parse(res.data);
            if (data.success == true) {
              that.setData({
                logoImageId: data.list[0]
              })
            } else {
              app.tip(res.data.message, "none");
            }
            wx.hideLoading();
          },
          fail: function (res) {
            wx.hideLoading();
            console.log('上传失败');
          }
        })

        if (that.data.logo.length != 0) {
          var logo = that.data.logo;
          logo.splice(0, 1)
          that.setData({
            logo: res.tempFilePaths
          })
        } else {
          that.setData({
            logo: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '',
      content: '确定要删除吗？',
      cancelText: '再看看',
      confirmText: '再见',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList,
            permitUrl: '',
            permitImageId: ''
          })
        }
      }
    })
  },

  bindinputPhone: function (e) {
    this.setData({ phone: e.detail.value });
  },

  bindinputCompanyName: function (e) {
    this.setData({ companyName: e.detail.value });
  },

  bindinputContent: function (e) {
    this.setData({ content: e.detail.value });
  },

  bindinputLinkMan: function (e) {
    this.setData({ linkMan: e.detail.value });
  },

  bindinputEmail: function (e) {
    this.setData({ email: e.detail.value });
  },

  bindinputAddress: function (e) {
    this.setData({ address: e.detail.value });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var phoneNumber = wx.getStorageSync("phoneNumber")
    that.setData({
      phone: phoneNumber
    });
    wx.request({
      url: config.queryIndustryTypeList,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {},
      success: function (result) {
        console.info("行业类型查询返回结果");
        console.info(result);
        var data = result.data.object;
        var arrayObj = new Array();
        if (data != null) {
          for (var i = 0; i < data.length; i++) {
            arrayObj.push(data[i].industryTypeName);
          }

          that.setData({
            industryTypeArray: data,
            industryTypeName: data[0].industryTypeName,
            industryTypeId: data[0].industryTypeId,
            industryTypeNameArray: arrayObj
          });
        } else {
          app.tip(result.data.message, "none");
        }

      },
      fail: function ({ errMsg }) {

      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})